
#include <iostream>
#include <memory.h>
#pragma once

namespace DTree
{
	const char TREE_DELIMITER	= '\t';
	const char TREE_BRANCH		= '>';
	const char TREE_LEAF		= '^';

	////////////////////////////////////////////////////////////////////////////////
	/*!	\class		TreeFileIO
	**	\brief		Saves and loads trees to/from file.
	**	\author		Nate Cleveland
	**	\date		11/19/2002 12:39:46 PM
	**	\warning	none
	*/
	////////////////////////////////////////////////////////////////////////////////
	template <class Node_Type>
	class TreeFileIO
	{
	private:
		void WriteBranch(const Node_Type& node, std::ostream& outStream);
		void WriteLeaf(const Node_Type& node, std::ostream& outStream);
		void ReadBranch(Node_Type& node, const char* buffer);//std::istream& inStream);
		void ReadLeaf(Node_Type& node, const char* buffer);//std::istream& inStream);


	protected:

		//feeds in the relevent ammount of Delimiters
		void FeedDelimiter(unsigned int tabCount, std::ostream& outStream) throw()
		{
			for(unsigned int i = 0 ; i < tabCount ; i++)
				outStream << TREE_DELIMITER;
		}

		//Removes the delimiters.
		void StripDelimiter(unsigned int tabCount, char* buffer);//std::istream& inStream);


		//Saves a tree to the stream.
		void SaveTree(unsigned int tabCount, const Node_Type& root, std::ostream& outStream);

		//Loads a tree from the stream.
		void LoadTree(unsigned int tabCount,Node_Type*& root, std::istream& inStream);

	public:
		//Saves a Tree to file.
		TreeFileIO(const Node_Type* root, std::ostream& outStream) ;

		//Loads a tree in from file
		TreeFileIO(Node_Type*& root, std::istream& inStream) ;
	};	

	/*!
	**	Since Visual C++ 6.0 does not support the export keyword we must use a hack around.
	**	 The hack is to #include the .cpp file. Ugly and evil, but it is the only option if
	**	 we wish to use a generic templitized method. If this file is ever included into a 
	**	 non-Visual C++ (or borland) workspace this hack should be removed and the 
	**	 keyword "export" added before each instance of the world "template" in 
	**	 treeFileIO.cpp.
	*/
#	include "treeFileIO.cpp"	
}